package cn.iocoder.yudao.module.trade.controller.admin.lifecircle;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.trade.controller.admin.lifecircle.vo.*;
import cn.iocoder.yudao.module.trade.controller.app.lifecircle.vo.AppLifeCircleStoreTypeRespVO;
import cn.iocoder.yudao.module.trade.convert.lifecircle.LifeCircleStoreConvert;
import cn.iocoder.yudao.module.trade.dal.dataobject.lifecircle.LifeCircleStoreDO;
import cn.iocoder.yudao.module.trade.dal.dataobject.lifecircle.LifeCircleStorePointsDO;
import cn.iocoder.yudao.module.trade.service.lifecircle.LifeCircleStoreService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 生活圈管理-店铺管理")
@RestController
@RequestMapping("/trade/life-circle/store")
@Validated
public class LifeCircleStoreController {

    @Resource
    private LifeCircleStoreService lifeCircleStoreService;

    @PostMapping("/create")
    @Operation(summary = "创建店铺")
    @PreAuthorize("@ss.hasPermission('trade:life-circle:store:create')")
    public CommonResult<Long> createLifeCircleStore(@Valid @RequestBody LifeCircleStoreCreateReqVO createReqVO) {
        return success(lifeCircleStoreService.createLifeCircleStore(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新店铺")
    @PreAuthorize("@ss.hasPermission('trade:life-circle:store:update')")
    public CommonResult<Boolean> updateLifeCircleStore(@Valid @RequestBody LifeCircleStoreUpdateReqVO updateReqVO) {
        lifeCircleStoreService.updateLifeCircleStore(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除店铺")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('trade:life-circle:store:delete')")
    public CommonResult<Boolean> deleteLifeCircleStore(@RequestParam("id") Long id) {
        lifeCircleStoreService.deleteLifeCircleStore(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店铺")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:life-circle:store:query')")
    public CommonResult<LifeCircleStoreRespVO> getLifeCircleStore(@RequestParam("id") Long id) {
        LifeCircleStoreDO deliveryPickUpStore = lifeCircleStoreService.getLifeCircleStore(id);
        if (deliveryPickUpStore == null) {
            return success(null);
        }
        List<Long> pointsIds = deliveryPickUpStore.getPointsIds();
        LifeCircleStoreRespVO lifeCircleStoreRespVO = BeanUtils.toBean(deliveryPickUpStore, LifeCircleStoreRespVO.class);
        lifeCircleStoreRespVO.setStoreImages(deliveryPickUpStore.getStoreImages());
        if (!CollectionUtils.isEmpty(pointsIds)) {
            lifeCircleStoreRespVO.setPoints(lifeCircleStoreService.getPointsListByIds(pointsIds));
        }else {
            lifeCircleStoreRespVO.setPoints(new ArrayList<>());
        }
        return success(lifeCircleStoreRespVO);
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得店铺精简信息列表")
    public CommonResult<List<LifeCircleStoreSimpleRespVO>> getSimpleLifeCircleStoreList() {
        List<LifeCircleStoreDO> list = lifeCircleStoreService.getLifeCircleStoreListByStatus(CommonStatusEnum.ENABLE.getStatus());
        return success(LifeCircleStoreConvert.INSTANCE.convertList1(list));
    }

    @GetMapping("/list")
    @Operation(summary = "获得店铺列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('trade:life-circle:store:query')")
    public CommonResult<List<LifeCircleStoreRespVO>> getLifeCircleStoreList(@RequestParam("ids") Collection<Long> ids) {
        List<LifeCircleStoreRespVO> list = lifeCircleStoreService.getLifeCircleStoreList(ids);
        return success(list);
    }

    @GetMapping("/page")
    @Operation(summary = "获得店铺分页")
    @PreAuthorize("@ss.hasPermission('trade:life-circle:store:query')")
    public CommonResult<PageResult<LifeCircleStoreRespVO>> getLifeCircleStorePage(@Valid LifeCircleStorePageReqVO pageVO) {
        PageResult<LifeCircleStoreRespVO> pageResult = lifeCircleStoreService.getLifeCircleStorePage(pageVO);
        return success(pageResult);
    }

    @PostMapping("/bind")
    @Operation(summary = "绑定店铺积分")
    @PreAuthorize("@ss.hasPermission('trade:life-circle:store:create')")
    public CommonResult<Boolean> bindLifeCircleStore(@Valid @RequestBody LifeCircleBindReqVO bindReqVO) {
        lifeCircleStoreService.bindLifeCircleStore(bindReqVO);
        return success(true);
    }

    @GetMapping("/getPointsList")
    @Operation(summary = "获得积分列表")
    @PreAuthorize("@ss.hasPermission('trade:life-circle:store:query')")
    public CommonResult<PageResult<LifeCircleStorePointsDO>> getPointsList(@Valid LifeCircleStorePointsPageReqVO pageVO) {
        PageResult<LifeCircleStorePointsDO> list = lifeCircleStoreService.getPointsList(pageVO);
        return success(list);
    }

    @GetMapping("/getTypeList")
    @Operation(summary = "获得店铺分类列表")
    @PermitAll
    public CommonResult<List<AppLifeCircleStoreTypeRespVO>> getTypeList() {
        return success(lifeCircleStoreService.getTypeList());
    }
}
